function [DivorceCostsM,DivorceCostsF,DivorceCostsMF] = get_divorce_costs(leisurem,leisuref,hworkm,hworkf,consumption,wagem,wagef,nonlabor,ub)
%%
% LP to get the divorce costs (stability indices)
% Program uses YALMIP (https://yalmip.github.io) connected to CPLEX or GUROBI

% INPUT VARIABLES:
% leisurem[households]      -- leisure per week of the male
% leisuref[households]      -- leisure per week of the female
% hworkm[households]        -- time spend on home production by the male
% hworkf[households]        -- time spend on home production by the female
% consumption[households]   -- weekly Hicksian consumption
% wagem[households]         -- wage rate of the male
% wagef[households]         -- wage rate of the female
% nonlabor[households]      -- weekly nonlabor income of the household
% ub                        -- upper bound on economies of scale of leisure (aleisure)

% OUTPUT VARIABLES:
% DivorceCostsM  -- Divorce Costs for individual rationality of males
% DivorceCostsF  -- Divorce Costs for individual rationality of females
% DivorceCostsMF -- Divorce Costs for no blocking pairs 

%%
% ==================
% defining constants
% ==================

workh = 112;                                          % total potential working hours
num_of_hh = length(leisurem);                         % total number of households in the data.

% ===========================
% defining decision variables
% ===========================

privateqM = sdpvar(num_of_hh,1);                      % private consumption of Hicksian good by male
privateqF = sdpvar(num_of_hh,1);                      % private consumption of Hicksian good by female  
privatemhworkm = sdpvar(num_of_hh,1);                 % private consumption of male's domestic work by male
privatemhworkf = sdpvar(num_of_hh,1);                 % private consumption of male's domestic work by female
privatefhworkm = sdpvar(num_of_hh,1);                 % private consumption of female's domestic work by male
privatefhworkf = sdpvar(num_of_hh,1);                 % private consumption of female's domestic work by female  
privateleisurem = sdpvar(num_of_hh,1);                % private consumption of leisure by male
privateleisuref = sdpvar(num_of_hh,1);                % private consumption of leisure by female

aleisurem = sdpvar(1);                                % degree of publicness in leisure of male
aleisuref = sdpvar(1);                                % degree of publicness in leisure of female 
ahworkm = sdpvar(1);                                  % degree of publicness in domestic work of male  
ahworkf = sdpvar(1);                                  % degree of publicness in domestic work of female 
aq = sdpvar(1);                                       % degree of publicness in Hicksian good 

nl_incomeM = sdpvar(num_of_hh,1);                     % non-labor income of male  
nl_incomeF = sdpvar(num_of_hh,1);                     % non-labor income of female

divorce_costsM = sdpvar(num_of_hh,1);                 % divorce costs individual rationality male
divorce_costsF = sdpvar(num_of_hh,1);                 % divorce costs individual rationality female
divorce_costsMF = sdpvar(num_of_hh,num_of_hh,'full'); % divorce costs no blocking pairs


%% 

% ==========================
% setting up the constraints
% ==========================

Constraints = [];

% private consumption should be balanced:
for i=1:num_of_hh
    Constraints=[Constraints, ...
        consumption(i)*(1-aq) == privateqM(i) + privateqF(i), privateqM(i) >=0, privateqF(i) >= 0,...
        hworkm(i)*(1-ahworkm) == privatemhworkm(i) + privatemhworkf(i), privatemhworkm(i) >= 0, privatemhworkf(i) >= 0,...
        hworkf(i)*(1-ahworkf) == privatefhworkm(i) + privatefhworkf(i), privatefhworkm(i) >=0 , privatefhworkf(i) >= 0,...
        leisurem(i)*(1-aleisurem) == privateleisurem(i),...
        leisuref(i)*(1-aleisuref) == privateleisuref(i)];
end


% nonlabor income should be balanced:
for i=1:num_of_hh
         Constraints = [Constraints, nonlabor(i) == nl_incomeM(i)+ nl_incomeF(i)];
end

% bounding the possible sharing of nonlabor income between partners between [.4,.6]
for i=1:num_of_hh
        if nonlabor(i)>=0
            Constraints = [Constraints,
                .4*nonlabor(i) <= nl_incomeM(i) <= .6*nonlabor(i), .4*nonlabor(i) <= nl_incomeF(i) <= .6*nonlabor(i)];
        else 
            Constraints = [Constraints,
                .6*nonlabor(i) <= nl_incomeM(i) <= .4*nonlabor(i), .6*nonlabor(i) <= nl_incomeF(i) <= .4*nonlabor(i)];
        end
end

% Individual rationality conditions:
for i=1:num_of_hh
       % IR-Male
       Constraints = [Constraints,
           (wagem(i)*workh)*divorce_costsM(i) + nl_incomeM(i) <= privateleisurem(i)*wagem(i) + privatemhworkm(i)*wagem(i) + privatefhworkm(i)*wagef(i) + privateqM(i) + hworkm(i)*ahworkm*wagem(i) + hworkf(i)*ahworkf*wagef(i) + aq*consumption(i) + leisurem(i)*aleisurem*wagem(i) + leisuref(i)*aleisuref*wagef(i)];
       % IR-Female
       Constraints = [Constraints,
           (wagef(i)*workh)*divorce_costsF(i) + nl_incomeF(i) <= privateleisuref(i)*wagef(i) + privatemhworkf(i)*wagem(i) + privatefhworkf(i)*wagef(i) + privateqF(i) + hworkm(i)*ahworkm*wagem(i) + hworkf(i)*ahworkf*wagef(i) + aq*consumption(i) + leisurem(i)*aleisurem*wagem(i) + leisuref(i)*aleisuref*wagef(i)];
end


% No blocking pairs condition: 
for i=1:num_of_hh
    for j=1:num_of_hh
        if i ~= j
           Constraints = [Constraints, 
                (wagem(i) + wagef(j))*workh*divorce_costsMF(i,j) + nl_incomeM(i) + nl_incomeF(j) <= privateleisurem(i)*wagem(i) + privatemhworkm(i)*wagem(i) + privatefhworkm(i)*wagef(j) + privateqM(i) + privateleisuref(j)*wagef(j) + privatemhworkf(j)*wagem(i) + privatefhworkf(j)*wagef(j) + privateqF(j) + max(hworkm(i),hworkm(j))*ahworkm*wagem(i) + max(hworkf(i),hworkf(j))*ahworkf*wagef(j) + aq*max(consumption(i),consumption(j)) + max(leisurem(i),leisurem(j))*aleisurem*wagem(i) + max(leisuref(i),leisuref(j))*aleisuref*wagef(j)];
        end
    end
end
   
% Constraints on divorce costs
% individual rationality 
for i=1:num_of_hh
    Constraints = [Constraints, 0 <= divorce_costsM(i) <= 1, 0 <= divorce_costsF(i) <= 1];
end
% no blocking pairs
for i=1:num_of_hh
    for j=1:num_of_hh
         Constraints = [Constraints,  0 <= divorce_costsMF(i,j) <= 1];
    end
end

% Constraints on degree of publicness 
 Constraints = [Constraints,  0 <= aq <= 1, 0 <= aleisurem <= ub, 0 <= aleisuref <= ub, 0 <= ahworkm <= 1, 0 <= ahworkf <= 1];


%%
% ===================
% Solving the problem
% ===================

% Specifying the objective function
Objective = sum(divorce_costsM)+ sum(divorce_costsF)+ sum(divorce_costsMF(:));
options = sdpsettings('verbose',0,'solver','gurobi');

% Solve the problem
% -Objective because the default is to minimize while we want to maximize.
sol = optimize(Constraints,-Objective,options);

% Analyze error flags
if sol.problem ~= 0
 display('Something went wrong!');
 sol.info
 yalmiperror(sol.problem)
end

%getting the value out
DivorceCostsM  = value(divorce_costsM);
DivorceCostsF  = value(divorce_costsF);
DivorceCostsMF = value(divorce_costsMF);

return
